package org.summer.lawyer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.bo.brokeragerecord.BrokerageRecordBo;
import org.summer.lawyer.domain.vo.brokeragerecord.BrokerageRecordVo;
import org.summer.lawyer.service.IBrokerageRecordService;

import java.util.List;

/**
 * 佣金记录
 *
 * @author summer
 * @date 2024-06-25
 */
@Tag(name = "后台相关接口", description = "佣金记录")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/brokerageRecord")
public class BrokerageRecordController extends BaseController {

    private final IBrokerageRecordService brokerageRecordService;

    /**
     * 查询佣金记录列表
     */
    @Operation(summary = "查询佣金记录列表接口", description = "查询佣金记录列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:trade:list")
    @GetMapping("/list")
    public TableDataInfo<BrokerageRecordVo> list(BrokerageRecordBo bo, PageQuery pageQuery) {
        return brokerageRecordService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出佣金记录列表
     */
    @Operation(summary = "导出佣金记录列表接口", description = "导出佣金记录列表")
    @SaCheckPermission("brokerage:trade:export")
    @Log(title = "佣金记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BrokerageRecordBo bo, HttpServletResponse response) {
        List<BrokerageRecordVo> list = brokerageRecordService.selectBrokerageRecordVoList(bo);
        ExcelUtil.exportExcel(list, "佣金记录", BrokerageRecordVo.class, response);
    }

    /**
     * 获取佣金记录详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "获取佣金记录详细信息接口", description = "查询佣金记录列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:trade:query")
    @GetMapping("/{id}")
    public R<BrokerageRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long id) {
        return R.ok(brokerageRecordService.selectBrokerageRecordVoById(id));
    }

    /**
     * 新增佣金记录
     */
    @Operation(summary = "新增佣金记录接口", description = "新增佣金记录", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:trade:add")
    @Log(title = "佣金记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveBrokerageRecord")
    public R<Void> saveBrokerageRecord(@Validated(AddGroup.class) @RequestBody BrokerageRecordBo bo) {
        return toAjax(brokerageRecordService.insertByBo(bo));
    }

    /**
     * 修改佣金记录
     */
    @Operation(summary = "修改佣金记录接口", description = "修改佣金记录", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:trade:edit")
    @Log(title = "佣金记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateBrokerageRecord")
    public R<Void> updateBrokerageRecord(@Validated(EditGroup.class) @RequestBody BrokerageRecordBo bo) {
        return toAjax(brokerageRecordService.updateByBo(bo));
    }

    /**
     * 删除佣金记录
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除佣金记录接口", description = "删除佣金记录", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:trade:remove")
    @Log(title = "佣金记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeBrokerageRecord/{ids}")
    public R<Void> removeBrokerageRecord(@NotEmpty(message = "主键不能为空")
                                         @PathVariable Long[] ids) {
        return toAjax(brokerageRecordService.deleteWithValidByIds(List.of(ids), true));
    }
}
